<template>
    <div class="edit">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="学号" prop="number">
                <el-input v-model="form.number" type="number"></el-input>
            </el-form-item>
            <el-form-item label="所属班级" prop="classId">
                <el-select v-model="form.classId" placeholder="请选择班级">
                    <el-option v-for="item in options" :key="item.value" :label="item.name"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="生日" prop="birthday">
                <el-date-picker v-model="form.birthday" type="date" placeholder="选择生日"></el-date-picker>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="form.sex">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="0">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {
    classNames,
    studentAdd,
    studentEdit,
    getStudent,
} from "@/api/student.js"
export default {
    props: ['type', 'id'],
    data() {
        return {
            options: [],
            form: {
                id: -1,
                name: '',
                number: '',
                classId: '',
                password: '',
                email: '',
                birthday: '',
                sex: 1,
            },
            rules: {
                name: [
                    { require: true, message: '请输入姓名', trigger: 'blur' },
                ],
                number: [
                    { require: true, message: '请输入学号', trigger: 'blur' },
                    { min: 12, max: 12, message: '学号长度必须为12位', trigger: 'blur' },
                ],
                classId: [
                    { require: true, type: 'number', message: '请选择班级', trigger: 'blur' },
                ],
                password: [
                    { require: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, message: '密码长度必须大于6位', trigger: 'blur' },
                ],
                email: [
                    { require: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] },
                ],
                birthday: [
                    { require: true, type: 'date', message: '请选择生日', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        async getOptions() {
            let ret = await classNames()
            this.options = ret.data
        },
        async getName(id) {
            if (this.type === 'edit') {
                let ret = await getStudent(id);
                this.form = ret.data
                this.form.birthday = new Date(ret.data.birthday);
            }
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.type === 'add') {
                        studentAdd(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    } else if (this.type === 'edit') {
                        studentEdit(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    }
                }
            });
        },
        cancel() {
            this.$emit('cancel')
        },
    },
    watch: {
        id: {
            handler(newVal, oldVal) {
                this.getName(newVal)
            },
            immediate: true
        }
    },
    mounted() {
        this.getOptions()
    }
}
</script>